/**
 * Created by MacBook on 2019/3/29.
 */
    var imgSrc = []; //本地图片路径
    var imgFile = []; //文件流
    // 5 图片接口
    var images = {
        localId: [],
        serverId: []
    };

    //图片上传
    function upload(btnId) {
        wx.chooseImage({
            count: 9, // 默认9
            sizeType:'compressed',
            success: function (res) {
                images.localId = res.localIds;
                imgSrc = images.localId;
                //上传并将值设置在前端
                syncUpload(btnId,imgSrc);

            }
        });
    };

    //图片上传到服务器
    function syncUpload(btnId,imgSrc){
        imgFile = [];//清空数组的值
        var localId = imgSrc.pop();
        wx.uploadImage({
            localId: localId,
            isShowProgressTips: 1,
            sizeType:'compressed',
            success: function (res) {
                var serverId = res.serverId; // 返回图片的服务器端ID
                imgFile.push(serverId);

                //图片展示
                addNewContent(btnId,localId,serverId);

                //其他对serverId做处理的代码
                if (imgSrc.length > 0) {
                    syncUpload(btnId,imgSrc);
                }
            }
        });
    }


    //图片展示
    function addNewContent(btnId,localId,serverId) {
        var ua = navigator.userAgent.toLowerCase();
        var ios;
        console.log(ua);
        if (/iphone|ipad|ipod/.test(ua)) {
            ios = true;
        } else {
            ios = false;
        }
        //ios
        if (ios || window.__wxjs_is_wkwebview) {
            wx.getLocalImgData({
                localId: localId,
                success: function (res) {
                    var localData = res.localData; // localData是图片的base64数据，可以用img标签显示var str = '<div class="shangchuan_img_xx">'+
                    var oldBox = $("#"+btnId).html();
                    var imgbox_list = '<div class="imgContainer"><i class="imgbox_i_x" onclick="img_clear(this)">删除</i><img src=' + localData + ' onclick="imgDisplay(this)"><input name='+btnId+' type="hidden" value='+serverId+'></div>';
                    $("#"+btnId).html(oldBox + imgbox_list);
                },
                fail: function (res) {
                    alert('该图片暂时无法查看')
                }
            });
        }else{
            var oldBox = $("#"+btnId).html();
            var imgbox_list = '<div class="imgContainer"><i class="imgbox_i_x" onclick="img_clear(this)">删除</i><img src=' + localId + ' onclick="imgDisplay(this)"><input name='+btnId+' type="hidden" value='+serverId+'></div>';
            $("#"+btnId).html(oldBox + imgbox_list);
        }
    }


